package com.winter.service;

import com.google.common.base.Strings;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.FinManageMapper;
import com.winter.mapper.IcompanyCardMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.DictEntry;
import com.winter.model.FinContentLikeInfo;
import com.winter.model.FinManage;
import com.winter.model.FinUnderline;
import com.winter.model.IcompanyCard;
import com.winter.model.UserInfo;
import com.winter.model.vo.check.CheckParam;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
@Transactional
public class FinManageService {
    @Autowired
    public FinManageMapper finManageMapper;

    @Autowired
    public FinFavInfoMapper finFavInfoMapper;

    /**
     * 点赞
     */
    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;

    /**
     * 字典
     */
    @Autowired
    private DictEntryMapper dictEntryMapper;


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IcompanyCardMapper icompanyCardMapper;

    /**
     * 查询报价平台理财List信息
     */
    public Result finManageList(Integer pageNumber, Integer pageSize, String direct, String fund_mode, String loi, String termUnit, String userNo, String sortName, String sort, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<FinManage> finManageList = finManageMapper.finManageList(direct, fund_mode, loi, termUnit, sortName, sort, key);
        finManageList.forEach(f -> {
            String photoPath = null;
            String photoType = f.getPhotoType();
            if (!Strings.isNullOrEmpty(photoType)) {
                if (photoType.equals("0")) {
                    // 获取创建人头像
                    String createUser = f.getCreateUser();
                    UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                    if (userInfo != null) {
                        String custImgUrl = userInfo.getCustImgUrl();
                        if (!Strings.isNullOrEmpty(custImgUrl)) {
                            f.setAvatar(userInfo.getCustImgUrl());
                        }
                    } else {
                        //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                        //(int)(1+Math.random()*(10-1+1))
                        int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                        photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                        f.setAvatar(photoPath);
                    }
                } else {
                    //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                    //(int)(1+Math.random()*(10-1+1))
                    int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                    photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                    f.setAvatar(photoPath);
                }
            }


            f.setIsCollect("0");
            f.setIsLike("0");
            if (!Strings.isNullOrEmpty(userNo)) {
                // 点赞
                getIslikeOrCollect(userNo, f);

                // 获取标签名称
                getTypeName(f);

            }
        });
        PageInfo<FinManage> pageInfo = new PageInfo<>(finManageList);
        return Result.success(pageInfo);
    }

    /**
     * 通过id查询报价平台理财信息
     */
    public Result getFinManageId(Long id, String userNo) {
        FinManage fm = finManageMapper.selectByPrimaryKey(id);
        if (!Strings.isNullOrEmpty(userNo)) {
            getIslikeOrCollect(userNo, fm);
            // 获取标签名称
            getTypeName(fm);
        }
        return Result.success(fm);
    }


    /**
     * 获取字典实体名称
     */
    public void getTypeName(FinManage finManage) {
        if (!Strings.isNullOrEmpty(finManage.getDirect())) {
            // 方向
            DictEntry dictEntry = get("DC_FIN_DIRECTION", finManage.getDirect());
            if (dictEntry != null) {
                finManage.setDirect(dictEntry.getDictname());
            }
        }

        // 保本性质
        if (!Strings.isNullOrEmpty(finManage.getFundMode())) {
            DictEntry dictEntry = get("DC_BAOBEN_NATURE", finManage.getFundMode());
            if (dictEntry != null) {
                finManage.setFundMode(dictEntry.getDictname());
            }
        }
        //  保函
        if (!Strings.isNullOrEmpty(finManage.getLoi())) {
            DictEntry dictEntry = get("DC_GUARANTEE", finManage.getLoi());
            if (dictEntry != null) {
                finManage.setLoi(dictEntry.getDictname());
            }
        }
    }


    /**
     * 获取字典实体
     */
    public DictEntry get(String type, String chiType) {
        DictEntry dictEntry = dictEntryMapper.get(type, chiType);
        return dictEntry;
    }

    private void getIslikeOrCollect(String userNo, FinManage fm) {
        // 点赞
        List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(fm.getId(), "1", "3", userNo);
        if (isLike.size() > 0) {
            fm.setIsLike("1");
        }
        // 收藏
        Long aLong = finFavInfoMapper.get(fm.getId(), "1", 3, userNo);
        if (aLong > 0) {
            fm.setIsCollect("1");
        }
    }

    /**
     * 新增报价平台理财信息
     */
    public Result insertFinManage(FinManage fm) {
        if (fm != null) {
            if (Strings.isNullOrEmpty(fm.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
        }
        String s = CheckParam.checkFinManage(fm);
        if (!Strings.isNullOrEmpty(s)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, s);
        }

        UserInfo userInfo = userInfoMapper.selectByUserNo(fm.getCreateUser());
        String orgNo = userInfo.getOrgNo();
        IcompanyCard icompanyCard = icompanyCardMapper.selectByPrimaryKey(Long.valueOf(orgNo));
        // 公司信息
        fm.setUserName(userInfo.getUserName());
        fm.setUserCom(icompanyCard.getCompanyName());
        fm.setUserDepart(userInfo.getDepart());
        fm.setUserPost(userInfo.getPost());
        fm.setUserEmail(userInfo.getOrgEmail());
        fm.setUserQq(userInfo.getQq() + "");
        fm.setUserWx(userInfo.getWxNo());
        fm.setTopTime(new Date());
        fm.setInfoSrc("0");
        // 组装数据
        fm.setPhotoType("0");
        fm.setStas("3");
        fm.setAuditing("0");
        fm.setIsvalid("1");
        fm.setUpdatetime(new Date());
        fm.setCreatetime(new Date());
        fm.setCommentCount(new BigDecimal("0"));
        fm.setFavCount(new BigDecimal("0"));
        fm.setLikeCount(new BigDecimal("0"));
        fm.setCommentFlag("1");
        fm.setLikeFlag("1");
        fm.setReplayFlag("1");
        fm.setUpdateUser(fm.getCreateUser());
        int i = finManageMapper.insertSelective(fm);
        if (i > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failure(ResultCode.INSERT_ERROR);
        }
    }

    public Result updateFinManage(FinManage fm) {
        if (fm != null) {
            if (Strings.isNullOrEmpty(fm.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            if (fm.getId() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "主键id不能为空");
            }
            String s = CheckParam.checkFinManage(fm);
            if (!Strings.isNullOrEmpty(s)) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, s);
            }
            fm.setUpdatetime(new Date());
            finManageMapper.updateByPrimaryKeySelective(fm);
            return Result.success("修改成功");
        } else {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "参数列表为空");
        }
    }

}
